package com.logbug.concurrence;

import java.util.concurrent.TimeUnit;

public class ConcurrenceTest {

}

class SimpleTest {
    static boolean stop = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            int i = 0;
            while (!stop) {
                i++;
            }
            System.out.println("i = " + i);
        }).start();
        TimeUnit.SECONDS.sleep(1);
        stop = true;
    }
}
// i = 52656858
class SyncTest {
    static boolean stop = false;

    static synchronized boolean getStop() {
        return stop;
    }

    static synchronized void stop() {
        stop = true;
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            int i = 0;
            while (!getStop()) {
                i++;
            }
            System.out.println("i = " + i);
        }).start();
        TimeUnit.SECONDS.sleep(1);
        stop();
    }
}
// i = -1737465723
class VolatileTest {
    static volatile boolean stop = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            int i = 0;
            while (!stop) {
                i++;
            }
            System.out.println("i = " + i);
            System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE);
        }).start();
        TimeUnit.SECONDS.sleep(1);
        stop = true;
        System.out.println((int)0x00000000);
        System.out.println((int)0xffffffff);
    }
}